package LuckyDraw

import (
	"gitee.com/yebetter/lucky-draw/Conf"
	"gitee.com/yebetter/lucky-draw/Core/App/Log"
	"gitee.com/yebetter/lucky-draw/Core/Error"
	"gitee.com/yebetter/lucky-draw/Regulation"
	"gitee.com/yebetter/lucky-draw/Regulation/RuleProbabilityStore"
)

/**
抽奖算法工厂类
*/
type luckyDrawFactory struct {
	//抽奖算法
	typeRule Conf.LUCKY_DRAW_RULE_TYPE
	//抽奖算法对象
	ruleApi Regulation.RuleInterface

	//事件切面
	eventHandle Conf.EventHandle
	//奖项存储
	award     []*Conf.AwardItem
	container *drawContainer
}

func NewLuckyDrawFactory() LuckyDrawFactoryApi {
	luckyDrawFactoryApi := &luckyDrawFactory{}
	container := NewCrawContainer()
	//注册抽奖算法
	container.Add(RuleProbabilityStore.NewRuleProbabilityStore())

	luckyDrawFactoryApi.container = container
	return luckyDrawFactoryApi
}

/**
抽奖算法类型判断 int类型
*/
func (this *luckyDrawFactory) CheckTypeInt(typeInt int) bool {
	return Conf.LuckyDrawRuleTypeCheckInt(typeInt)
}

/**
抽奖算法类型判断 LUCKY_DRAW_RULE_TYPE 类型
*/
func (this *luckyDrawFactory) CheckType(typeRule Conf.LUCKY_DRAW_RULE_TYPE) bool {
	return Conf.LuckyDrawRuleTypeCheck(typeRule)
}
func (this *luckyDrawFactory) SetEventHandle(eventHandle Conf.EventHandle) {
	this.eventHandle = eventHandle
}

/**
设置抽奖类
*/
func (this *luckyDrawFactory) SetType(typeRule Conf.LUCKY_DRAW_RULE_TYPE) {
	this.typeRule = typeRule
}

func (this *luckyDrawFactory) SetTypeInt(typeInt int) {
	switch typeInt {
	case Conf.LUCKY_DRAW_RULE_INT_PROBABILITY_STORE:
		this.typeRule = Conf.LUCKY_DRAW_RULE_TYPE_PROBABILITY_STORE
	default:
		this.typeRule = Conf.LUCKY_DRAW_RULE_TYPE_PROBABILITY_STORE
	}
}

func (this *luckyDrawFactory) getApi() error {
	ruleApi, err := this.container.Get(this.typeRule)
	if err != nil {

		return err
	}
	this.ruleApi = ruleApi
	//this.ruleApi = RuleProbabilityStore.NewRuleProbabilityStore()
	return nil
}

func (this *luckyDrawFactory) SetAward(award []*Conf.AwardItem) {
	this.award = award
}

func (this *luckyDrawFactory) Handle() (*Conf.AwardResult, *Error.Errno) {
	err := this.getApi()
	if err != nil {
		Log.AppLog.Error("抽奖错误%s", err.Error())
		return Conf.LOSING_LOTTERY, nil
	}
	this.ruleApi.SetAward(this.award)
	this.ruleApi.SetEventHandle(this.eventHandle)
	return this.ruleApi.Handle()
}
